<?php

declare(strict_types=1);

use App\Migration\MigrationInterface;
use PDO;

return new class implements MigrationInterface {
    public function getId(): string
    {
        return '20250208000200_create_webhook_requests_table';
    }

    public function up(PDO $pdo): void
    {
        $pdo->exec(
            <<<'SQL'
            CREATE TABLE IF NOT EXISTS webhook_requests (
                id SERIAL PRIMARY KEY,
                webhook_id INT NOT NULL REFERENCES webhooks(id) ON DELETE CASCADE,
                method VARCHAR(16) NOT NULL,
                scheme VARCHAR(10) NOT NULL,
                host VARCHAR(255) NOT NULL,
                path TEXT NOT NULL,
                query_params JSONB NOT NULL DEFAULT '{}'::jsonb,
                headers JSONB NOT NULL DEFAULT '{}'::jsonb,
                body TEXT DEFAULT NULL,
                ip_address VARCHAR(45) DEFAULT NULL,
                user_agent TEXT DEFAULT NULL,
                created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
            );

            CREATE INDEX IF NOT EXISTS idx_webhook_requests_webhook_id_created_at
                ON webhook_requests (webhook_id, created_at DESC);
            SQL,
        );
    }

    public function down(PDO $pdo): void
    {
        $pdo->exec('DROP TABLE IF EXISTS webhook_requests');
    }
};
